# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(plyr)
library(gridExtra)
library(scales)
library(egg)
library(VennDiagram)
library(optparse)
library(tidyverse)
library(magrittr)

createWhenNoExist <- function(f) {
  !dir.exists(f) && dir.create(f)
}

option_list <- list(
  make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
  make_option("--config", default = "config.csv", type = "character", help = "config file")
)
opt <- parse_args(OptionParser(option_list = option_list))

configData <- read.csv(opt$config, header = F, stringsAsFactors = F) %>%
  set_colnames(c("arg", "value")) %>%
  column_to_rownames("arg")

borutaAfterTopN <- configData["borutaAfterTopN", "value"] %>%
  as.numeric()

fileName <- "Decision_Info.csv"

if (!file.exists(fileName)) {
  quit(status = 0)
}

diffNames <- read_csv(fileName) %>%
  rename(Metabolite = X1) %>%
  filter(decision == "Confirmed") %>%
  head(borutaAfterTopN) %>%
  .$Metabolite

if (length(diffNames) < 2) {
  diffNames <- read_tsv("../../potential/boruta_after.txt") %>%
    .$Metabolite
}

parent <- paste0("./")
createWhenNoExist(parent)

outData <- tibble(Metabolite = diffNames)

write_tsv(outData, "true_boruta_after.txt")





